package com.ahstu.learning.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ahstu.learning.R;
import com.ahstu.learning.bean.User;
import com.ahstu.learning.event.ChatEvent;
import com.ahstu.learning.friend.FriendTable;
import com.ahstu.learning.model.UserModel;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.v3.listener.SaveListener;

public class SearchUserHolder extends BaseViewHolder {

    @Bind(R.id.avatar)
    public ImageView avatar;
    @Bind(R.id.name)
    public TextView name;
    @Bind(R.id.btn_addfriend)
    public Button btn_addfriend;

    public SearchUserHolder(Context context, ViewGroup root) {
        super(context, root, R.layout.item_search_user, null);
    }

    @Override
    public void bindData(Object o) {  
        final User user = (User) o;
        name.setText(user.getUsername());
        btn_addfriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //添加好友功能实现成功
                FriendTable friendTableMy = new FriendTable();
                FriendTable friendTableFriend = new FriendTable();
                User userMy = UserModel.getInstance().getCurrentUser();

                friendTableMy.setMyName(userMy.getUsername());
                friendTableMy.setFriendName(user.getUsername());

                friendTableFriend.setMyName(user.getUsername());
                friendTableFriend.setFriendName(userMy.getUsername());
                //更新我的好友列表
                friendTableMy.save(getContext(), new SaveListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(getContext(), "添加失败", Toast.LENGTH_SHORT).show();
                    }
                });
                //更新被加的账号的好友列表
                friendTableFriend.save(getContext(), new SaveListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(getContext(), "添加失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}